


class myQ {
    constructor() {
        this.q = []
    }
    task(fn,time) {
        this.q.push({
            time,
            fn
        })
        return this
    }
    start() {
        //? 这里为什么要这样？因为需要then来保证串行
        let result = Promise.resolve();
        this.q.forEach((item) => {
            //? result = result.then 使其必须等待前面的resolve执行后才能继续执行下一个then
            result = result.then(() => {
                return new Promise((resolve, reject) => {
                    setTimeout(() => {
                        resolve(item.fn());
                    }, item.time);
                });
            });
        })
        return result
    }
}

new myQ().task(() => {
    console.log(1)
},1000).task(() => {
    console.log(2)
},2000).task(() => {
    console.log(3)
},1000).start()
